package com.ruihang.generalibrary.utils;

import android.os.SystemClock;
import android.support.v4.util.ArrayMap;
import android.text.TextUtils;

/**
 * Created by liuyu on 2017/9/20.
 */

public class TimeObserve {
    private static TimeObserve instance;

    private ArrayMap<String, Long> timeTemp;

    private TimeObserve() {
        timeTemp = new ArrayMap<>();
    }

    public boolean isEarly(String tag, long interval) {
        if (TextUtils.isEmpty(tag)) {
            return false;
        }
        Long l = timeTemp.get(tag);
        boolean b = false;
        long cur = SystemClock.elapsedRealtime();
        if (l != null) {
            b = cur - l <= interval;
        }
        timeTemp.put(tag, cur);
        return b;
    }

    public void removeObser(String tag) {
        timeTemp.remove(tag);
    }


    public static TimeObserve getInstance() {
        if (instance == null) {
            instance = new TimeObserve();
        }
        return instance;
    }

    public static boolean isEarlyS(String tag, long interval){
        return getInstance().isEarly(tag, interval);
    }

    public static void removeObserS(String tag) {
        getInstance().removeObser(tag);
    }

    public static void removeObserS(String... tags) {
        if (tags==null || tags.length==0){
            return;
        }
        getInstance();
        for (int i = 0; i < tags.length; i++) {
            instance.removeObser(tags[i]);
        }
    }
}
